//
//  ResetPassViewController_1.m
//  specialine
//
//  Created by 往返网络 on 17/2/7.
//  Copyright © 2017年 com. All rights reserved.
//

#import "ResetPassViewController_1.h"
#import "ResetPassWordViewController_3.h"
#import "AppDelegate.h"
#import "ResetPassView2.h" //view
#import "PublicMethod.h"
#import "SendViewController.h"
#import "ReceiveViewController.h"
#import "MineViewContrller.h"

//定义校验验证码URL
#define URL_CHECKYANGZHENGCODE [NSString stringWithFormat:@"%@/Appcustomernew/GetPassword/valiateCode?type=get",BASE_URL]
//定义获取验证码URL
#define URL_GETPASSWORDCODE [NSString stringWithFormat:@"%@/Appcustomernew/GetPassword/getCode?type=get",BASE_URL]

#define ResetEnterType @"loginaccess"  //进入重设密码页面（1）／手机验证页面（2）

@interface ResetPassViewController_1 ()<UITextFieldDelegate,ResetPassView2Delegate>

@property (nonatomic,strong)AFHTTPSessionManager * manger;

@end

@implementation ResetPassViewController_1


- (void)viewWillAppear:(BOOL)animated{

    [super viewWillAppear:animated];

    self.navigationController.navigationBarHidden = YES;
    


}

- (void)viewDidDisappear:(BOOL)animated{
    
    
    NSArray * subviewsarr = self.view.subviews;
    
    ResetPassView2 * resetview2 = [[ResetPassView2 alloc]init];
    
    for (UIView * view1 in subviewsarr) {
        
        if ([view1 isKindOfClass:[resetview2 class]]) {
            
            NSLog(@"stop");
            
            [resetview2 stopTimer];
            
            
        }
    }
    
    
}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    [self createUI];
    
    
    
    // Do any additional setup after loading the view.
}

- (void)viewWillDisappear:(BOOL)animated{
    
    [super viewWillDisappear:animated];
    
    self.navigationController.navigationBarHidden = NO;

}

- (void)createUI{
    

    ResetPassView2 * view2 = [[ResetPassView2 alloc]initWithFrame:CGRectMake(0, 0, SCREEN_W, SCREEN_H) andPhonestring:_phonestring];
    
    view2.Resetpass2Delegate = self;
    
    [self.view addSubview:view2];
    
    self.view.backgroundColor = [UIColor whiteColor];
    

}


//进入下一页/返回登陆界面
#pragma mark -- -- - - － － - - - - -- 协议代理方法 - -- - - -- - -- -- - - -- -

- (void)enterResetPassVC2BtnType:(ResetPassView2BtnType)resetPassType andCodestring:(NSString *)codestring{

    //保存验证码
    self.msgcode = codestring;

    switch (resetPassType) {
            
        case ResetPassView2BtnClickReturn:
            
            //回到登陆页面
            
            [self.navigationController dismissViewControllerAnimated:YES completion:nil];
            
            break;
            
        case ResetPassView2BtnClickNextBtn:
            
        {
            //判断进入主页还是 进入下一页
            [USERDEFAULTS objectForKey:ResetEnterType];
            
            //重设密码进入。。。
            if ([[USERDEFAULTS objectForKey:ResetEnterType] isEqualToString:@"1"]) {
                
                //进入下一页
                NSLog(@"重设密码进入");
                
                //检查是否为6位
                [self checkYanzhengCodeChange:codestring];
                
            }
            else if ([[USERDEFAULTS objectForKey:ResetEnterType] isEqualToString:@"2"]){
            
                //手机验证页面 进入主页 。。。
                NSLog(@"手机验证进入");

                // 校验验证码
                
                [self checkYanzhengCode:codestring];
                
                
            }
            
            break;
            
        }
            
       case ResetPassView2BtnClickAgainCodeBtn:
            
        {
            //重新发送验证码
            [self getCode:self.phonestring];
            
            
            break;
        }
            
            
        default:
            
            
        break;
            
    }
    
   
}
#pragma mark  - --- - - - 更改密码的校验验证码 － － －－ －  －
- (void)checkYanzhengCodeChange:(NSString *)codestring{
    
    //字符串反转之后，再发送出去 phonenum --- 反转 －－－＊2 －－－ 转化为a-z
    
    //开始动画
    UIImageView * im = [PublicMethod startAnimation];
    
    
    NSString * url = [NSString stringWithFormat:@"%@&mobile=%@&msgCode=%@",URL_CHECKYANGZHENGCODE,self.phonestring,codestring];
    
    NSLog(@"校验 验证码 url %@",url);
    
    [self.manger GET:url parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        
        if (responseObject) {
            
            NSDictionary * dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
            
            NSInteger code = [dict[@"code"]integerValue];
            
            if (code == 0) {
                
                //获取验证码成功
                
                [im stopAnimating];
                
                NSLog(@"get code ok");
                
                ALERT(@"提示", dict[@"info"], self, @"确定", nil);
                
             
                
                ResetPassWordViewController_3 * resetvc3 = [[ResetPassWordViewController_3 alloc]init];
                
                //重设密码需要手机号，验证码参数。.
                
                resetvc3.phonestring =  self.phonestring;
                
                resetvc3.msgcode = self.msgcode;
                
                [self.navigationController pushViewController:resetvc3 animated:YES];
                

                
                
            }else{
                
                [im stopAnimating];
                
                NSLog(@"get code no");
                
                ALERT(@"提示", dict[@"info"], self, @"确定", nil);
                
            }
            
            
        }
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        [im stopAnimating];
        
        ALERT(@"提示", @"网络请求失败", self, @"确定", nil);
        
    }];
    
    
}









#pragma mark  - -- - - -- -- -手机验证码登录的 －－  校验验证码  ＝ ＝ ＝ ＝ ＝ ＝ ＝ ＝ ＝＝ ＝ ＝
- (void)checkYanzhengCode:(NSString *)codestring{

    //字符串反转之后，再发送出去 phonenum --- 反转 －－－＊2 －－－ 转化为a-z
    
    //开始动画
    UIImageView * im = [PublicMethod startAnimation];
    
    NSString * url = [NSString stringWithFormat:@"%@&mobile=%@&msgCode=%@",URL_CHECKYANGZHENGCODE,self.phonestring,codestring];
    
    NSLog(@"手机验证码登录 url %@",url);
    
    [self.manger GET:url parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        
        if (responseObject) {
            
            NSDictionary * dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
            
            NSInteger code = [dict[@"code"]integerValue];
            
            if (code == 0) {
                
                //获取验证码成功
                
                [im stopAnimating];
                
                NSLog(@"get code ok");
                
                ALERT(@"提示", dict[@"info"], self, @"确定", nil);
                
                //存储本地信息
                
                NSUserDefaults * userdefault = [NSUserDefaults standardUserDefaults];
                
                [userdefault setObject:@"islogin" forKey:USERDEFAULTS_ISLOGIN];
                
                [userdefault setObject:self.phonestring forKey:USERDEFAULTS_PHONE];
                
                // 存储tocken
                [userdefault setObject:dict[@"token"] forKey:USER_TOCKE];
                
                NSLog(@"++++++手机验证登录 获得token %@",dict[@"token"]);
                NSLog(@"++++++手机验证登录 获得dict  %@",dict);

                //发送通知，刷新主页面的余额各种信息
                //发送主页刷新余额通知
                [[NSNotificationCenter defaultCenter]postNotificationName:@"REFRESH" object:nil];
                //进入主页
                
                [self.navigationController dismissViewControllerAnimated:YES completion:^{
                    
                    [PublicMethod enterNewMain];
                    
                }];
                
                
            }else{
                
                [im stopAnimating];
                
                NSLog(@"get code no");
                
                ALERT(@"提示", dict[@"info"], self, @"确定", nil);
                
            }
            
            
        }
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        [im stopAnimating];
        
        ALERT(@"提示", @"网络请求失败", self, @"确定", nil);
        
    }];


}
#pragma mark  - -- - - - -   获取验证码 - - - - - - - - - --
- (void)getCode:(NSString * )phonestr{
    
    
    //字符串反转之后，再发送出去 phonenum --- 反转 －－－＊2 －－－ 转化为a-z
    
    NSMutableString *s = [NSMutableString string];
    
    for (NSUInteger i = phonestr.length; i>0; i--) {
        
        [s appendString:[phonestr substringWithRange:NSMakeRange(i-1, 1)]];
        
    }
    
    NSInteger s1 = [s integerValue]*2;
    
    NSString * s2 = [NSString stringWithFormat:@"%ld",s1];
    
    NSLog(@"s2 %@",s2);
    
    NSArray * arr = @[@"a",@"b",@"c",@"d",@"e",@"f",@"g",@"h",@"i",@"j"];
    
    NSMutableString * endstring = [NSMutableString string];
    
    for (NSUInteger i = 0; i < s2.length; i++) {
        
        //截取1个字符串
        NSString * s3 =  [s2 substringWithRange:NSMakeRange(i, 1)];
        
        NSInteger loc = [s3 integerValue];
        
        // 转化为小写字母
        
        NSString * s4 = arr[loc];
        
        [endstring appendString:s4];
        
        
    }
    
    //开始动画
    UIImageView * im = [PublicMethod startAnimation];
    
    NSLog(@"endstring %@",endstring);
    
    NSString * url = [NSString stringWithFormat:@"%@&mobile=%@",URL_GETPASSWORDCODE,endstring];
    
    NSLog(@"get 验证码 url %@",url);
    
    [self.manger GET:url parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        
        if (responseObject) {
            
            NSDictionary * dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
            
            NSInteger code = [dict[@"code"]integerValue];
            
            if (code == 0) {
                
                //获取验证码成功
                
                [im stopAnimating];
                
                NSLog(@"get code ok");
                
                ALERT(@"提示", dict[@"info"], self, @"确定", nil);
                
                
                
                
            }else{
                
                [im stopAnimating];
                
                NSLog(@"get code no");
                
                ALERT(@"提示", dict[@"info"], self, @"确定", nil);
                
            }
            
            
        }
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        [im stopAnimating];
        
        ALERT(@"提示", @"网络请求失败", self, @"确定", nil);
        
    }];
    
    
}

- (AFHTTPSessionManager*)manger{
    
    _manger = [AFHTTPSessionManager manager];
    
    _manger.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    _manger.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"application/json", nil];
    
    return _manger;
    
    
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
